import pandas as pd

df = pd.read_excel('朴素贝叶斯数据集.xlsx', index=False)
label = list(df)
dic_p = {}
for i in ['A', 'B', 'C', 'D']:
    num = df[df['Rating']==i]['Rating'].count()
    for j in label[:-1]:
        each = df[df['Rating']==i][j].sum()
        dic_p['%s|%s'%(j,i)] = each/num
print(dic_p)
# 预测项
X = {'Biography': 1,
     'Drama': 1,
     'Thriller': 0,
     'Comedy': 0,
     'Crime': 0,
     'Mystery': 0,
     'History': 1}
X1 = {}
for label in X:
    X1[label+'|A'] = X[label]
p = 8/30
for i in X1:
    if X1[i]:
        p *= dic_p[i]
        print(i, dic_p[i])
    else:
        p *= 1 - dic_p[i]
        print(i, 1 - dic_p[i])
print(p)
